<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class users extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(array(
            'login_model',
            'auth_model',
            'home_model'));
    }
    
    public function index($target = 0)
    {
        $userList = $this->auth_model->userList($target);
        $breakCrum = '<a href="' . base_url() . '">Admin</a> » <span>User</span>';
        $content = $this->load->view('users/main', $userList, true);
        $this->load->view('templates/admin', array('content' => $content, 'breakCrum' =>
                $breakCrum));
    }
    
    public function add()
    {
        $data = array("content" => $this->load->view("users/add", '', true));
        $this->load->view("templates/admin", $data);
    }
    
    public function edit($id)
    {
        if (!$this->auth_model->isUser($id)) {
            redirect(site_url('users'));
        } else {
            $breakCrum = '<a href="' . base_url() . '">Admin</a> » <a href="' . site_url('users') .
                '">Users</a> » <span>Update</span>';
            $content = $this->load->view('users/edit', $this->auth_model->GetUserById($id), true);
            $this->load->view('templates/admin', array('content' => $content, 'breakCrum' =>
                    $breakCrum));
        }
    }
    
    public function delete($id)
    {
        if (!$this->auth_model->isUser($id)) {
            redirect(site_url('users'));
        } else {
            $this->db->delete('users', array('id' => $id, 'idel' => 'Yes'));
            return redirect(site_url("users"));
        }
    }

    public function doAdd()
    {
        if (!$this->auth_model->checkFormAdd()) {
            notify_set_messages(validation_errors(), 'users');
            return $this->add();
        }
        if ($this->auth_model->doAdd()) {
            notify_set_messages(t('User have been added!'), 'users');
            return redirect(site_url("users"));
        }

    }

    public function update()
    {
        if (!$this->auth_model->checkForm()) {
            notify_set_messages(validation_errors(), 'users');
            return $this->edit(decrypt($_POST["idx"]));
        }
        if ($this->auth_model->doUpdate()) {
            notify_set_messages(t('User have been updated!'), 'users');
            redirect(site_url("users"));
        }
    }
    
    public function changepass()
    {
        $breakCrum = '<a href="' . base_url() . '">Admin</a> » <a href="' . site_url('users') .
            '">Users</a> » <span>Change password</span>';
        $content = $this->load->view('users/changepass', $this->auth_model->GetUserById
            ($this->session->userdata('USID')), true);
        $this->load->view('templates/admin', array('content' => $content, 'breakCrum' =>
                $breakCrum));
    }
    
    function dochangepass()
    {
        if (!$this->auth_model->checkFormPass()) {
            notify_set_messages(validation_errors(), 'users');
            return $this->changepass();
        }
        if ($this->auth_model->doChangePass()) {
            notify_set_messages(t('Password have been changed!'), 'users');
            redirect(site_url("users"));
        }
    }
}
